<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="角色名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getRoles">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="roles" highlight-current-row v-loading="listLoading"
                  style="width: 100%;" height="650" :row-class-name="tableRowClassName" @selection-change="selChange">
            <el-table-column type="selection" width="90">
            </el-table-column>
            <el-table-column type="index" width="70">
            </el-table-column>
            <el-table-column prop="name" label="角色名称" width="210" sortable>
            </el-table-column>
            <el-table-column prop="description" label="角色描述" width="210" sortable>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="210" sortable>
            </el-table-column>
            <el-table-column prop="isUse" label="是否使用" min-width="150" :formatter="formatIsUse" sortable>
            </el-table-column>
            <el-table-column label="操作" width="210">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    <el-button type="primary" size="small" @click="handleAuth(scope.$index, scope.row)">授权</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page"
                    :page-sizes="[20, 50, 100, 500, 1000]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                    <el-input v-model="editForm.description"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker type="date" placeholder="选择日期" v-model="editForm.createTime"></el-date-picker>
                </el-form-item>
                <el-form-item label="是否使用" prop="isUse">
                    <el-radio-group v-model="editForm.isUse">
                        <el-radio class="radio" label="Y">是</el-radio>
                        <el-radio class="radio" label="N">否</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                    <el-input v-model="addForm.description"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker type="date" placeholder="选择日期" v-model="addForm.createTime"></el-date-picker>
                </el-form-item>
                <el-form-item label="是否使用" prop="isUse">
                    <el-radio-group v-model="addForm.isUse">
                        <el-radio class="radio" label="Y">是</el-radio>
                        <el-radio class="radio" label="N">否</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!-- 权限配置对话框 -->
        <el-dialog :visible.sync="permissionDialogFormVisible" title="权限配置">
            <el-tree
                    ref="tree"
                    :data="systemPermissions"
                    :default-checked-keys="assignedPermissions"
                    show-checkbox
                    node-key="id"
                    highlight-current>
                    <span slot-scope="{ node, data }">
                      <span>{{ data.label }}</span>
                      <el-tag v-if="data.api" type="success" size="mini">{{ data.api }}</el-tag>
                    </span>
            </el-tree>
            <div slot="footer" class="dialog-footer">
                <el-button @click="permissionDialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="updatePermission">确定</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/js/util'
    import {getRoleListPage, removeRole, batchRemoveRole, editRole, addRole, getPermission, updatePermission} from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                roles: [],
                total: 0,
                pageSize: 20,
                page: 1,
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入角色名称', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入角色描述', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    name: '',
                    description: '',
                    createTime: '',
                    isUse: ''
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入角色名称', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入角色描述', trigger: 'blur'}
                    ],
                    isUse: [
                        {required: true, message: '请选择是否使用', trigger: 'change'}
                    ],
                    createTime: [
                        {type: 'date', required: true, message: '请输入创建日期', trigger: 'change'}
                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    description: '',
                    createTime: '',
                    isUse: ''
                },
                permissionDialogFormVisible: false,
                systemPermissions: null,
                assignedPermissions: null,
                permissionForm: {
                    roleId: undefined,
                    permissions: []
                }
            }
        },
        methods: {
            handleCurrentChange(val) {
                this.page = val;
                this.getRoles();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.getRoles();
            },
            //获取用户列表
            getRoles() {
                let para = {
                    pageSize: this.pageSize,
                    currentPage: this.page,
                    whereMap:
                        {
                            name: this.filters.name
                        }
                };
                this.listLoading = true;
                getRoleListPage(para).then((res) => {
                    this.total = res.data.data.totalRecord;
                    this.roles = res.data.data.resultObject;
                    this.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = row.id;
                    removeRole(para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getRoles();
                    });
                }).catch(() => {

                });
            },
            handleAuth: function (index, row) {
                this.permissionDialogFormVisible = true;
                this.permissionForm.roleId = row.id;
                let para = row.id;
                getPermission(para).then(response => {
                    this.systemPermissions = response.data.data.systemPermissions;
                    this.assignedPermissions = response.data.data.assignedPermissions;
                });
            },
            updatePermission: function () {
                this.permissionForm.permissions = this.$refs.tree.getCheckedKeys(true);
                updatePermission(this.permissionForm)
                    .then(response => {
                        this.permissionDialogFormVisible = false;
                        this.$message({
                            message: '授权成功',
                            type: 'success'
                        });
                    })
                    .catch(response => {
                        this.$message({
                            message: "失败",
                            type: 'error'
                        });
                    })
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    description: '',
                    createTime: '',
                    isUse: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            para.createTime = (!para.createTime || para.createTime == '') ? '' : util.formatDate.format(new Date(para.createTime), 'yyyy-MM-dd');
                            editRole(para).then((res) => {
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getRoles();
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            para.createTime = (!para.createTime || para.createTime == '') ? '' : util.formatDate.format(new Date(para.createTime), 'yyyy-MM-dd');
                            addRole(para).then((res) => {
                                this.addLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getRoles();
                            });
                        });
                    }
                });
            },
            selChange: function (selects) {
                console.log(selects);
                this.sels = selects;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {ids: ids};
                    batchRemoveRole(para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getRoles();
                    });
                }).catch(() => {

                });
            },
            tableRowClassName(row, rowIndex) {
                if (row.isUse == 'N') {
                    return 'warning-row';
                } else if (row.isUse == 'Y') {
                    return '';
                }
                return '';
            },
            formatIsUse(row, column) {
                if (row.isUse === 'N') {
                    return '禁用';
                } else if (row.isUse === 'Y') {
                    return '可使用';
                }
                return '';
            }
        },
        mounted() {
            console.log("钩子函数");
            this.getRoles();
        }
    }

</script>

<style>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>
